/**
 * 
 */
package br.com.ctm.advogadope.dao.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;

import br.com.ctm.advogadope.dao.api.AdvogadoDao;
import br.com.ctm.advogadope.model.Advogado;
import br.com.framework.persistencia.impl.BaseDaoImpl;
import br.com.framework.persistencia.model.AuditedEntity;
import br.com.framework.persistencia.model.AuditedEntity.Status;

/**
 * @author Cleber
 *
 */
@Stateless
public class AdvogadoDaoImpl extends BaseDaoImpl<Advogado> implements AdvogadoDao{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 */
	public AdvogadoDaoImpl() {
		super(Advogado.class);
	}
	
	@Override
	public List<Advogado> findByProcesso(String npu, int firstResult,
			int maxResults, boolean onlyActive) throws PersistenceException {
		StringBuffer qryStr = new StringBuffer();
		qryStr.append("SELECT adv FROM Advogado adv JOIN adv.processos p WHERE p.npu = :npu ");
		if (onlyActive && AuditedEntity.class.isAssignableFrom(getEntityClass())) {
	    	qryStr.append("AND adv.status = :status ");
	    }
		TypedQuery<Advogado> query = getEntityManager().createQuery(
				qryStr.toString(), Advogado.class);
		query.setParameter("npu", npu);
		if (onlyActive && AuditedEntity.class.isAssignableFrom(getEntityClass())) {
	    	query.setParameter("status", Status.ACTIVE);
	    }
		if (firstResult > -1 && maxResults > -1){
	    	query.setFirstResult(firstResult);
	    	query.setMaxResults(maxResults);
		}
		return query.getResultList();
	}
	
}
